package com.yunkaweilai.android.view.home;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.github.lazylibrary.util.DensityUtil;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.AppRuleModel;
import com.yunkaweilai.android.model.CountIncomeBean;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 统计界面店铺收入界面
 */
public class CountThirdView extends AbsHeaderView<CountIncomeBean.DataBean> {

    @BindView(R.id.rg_day)
    RadioGroup rgDay;//日期选择radiogroup
    @BindView(R.id.rb_yesterday)
    RadioButton rbYesterday;
    @BindView(R.id.rb_today)
    RadioButton rbToday;
    @BindView(R.id.rb_7day)
    RadioButton rb7day;
    @BindView(R.id.rb_30day)
    RadioButton rv30day;
    @BindView(R.id.tv_chart_count)
    TextView tvChartCount;//总计textView
    @BindView(R.id.view_chart1)
    View viewChart1;//要画的百分比条条
    @BindView(R.id.view_chart2)
    View viewChart2;//要画的百分比条条
    @BindView(R.id.view_chart3)
    View viewChart3;//要画的百分比条条
    @BindView(R.id.view_chart4)
    View viewChart4;//要画的百分比条条
    @BindView(R.id.tv_chart_1_top)
    TextView tvChart1Top;//第一个元素的上面的textView 现金
    @BindView(R.id.tv_chart_1_bottom)
    TextView tvChart1Bottom;//第一个元素的下面的textView  现金%
    @BindView(R.id.tv_chart_2_top)
    TextView tvChart2Top;//第二个元素的上面的textView  银行卡
    @BindView(R.id.tv_chart_2_bottom)
    TextView tvChart2Bottom;//第二个元素的下面的textView  银行卡%
    @BindView(R.id.tv_chart_3_top)
    TextView tvChart3Top;//第三个元素的上面的textView  支付宝
    @BindView(R.id.tv_chart_3_bottom)
    TextView tvChart3Bottom;//第三个元素的下面的textView  支付宝%
    @BindView(R.id.tv_chart_4_top)
    TextView tvChart4Top;//第四个元素的上面的textView  微信
    @BindView(R.id.tv_chart_4_bottom)
    TextView tvChart4Bottom;//第四个元素的下面的textView  微信%

    private String type = "";
    private CountIncomeBean IncomeBean;
    private final AppRuleModel.DataBean.InfoBean appRuleModel;


    public CountThirdView(Activity activity) {
        super(activity);
        appRuleModel = BaseApplication.getAppRuleModel();
    }

    @Override
    protected void getView(CountIncomeBean.DataBean dataBean, ListView listView) {
        View view = mInflate.inflate(R.layout.count_third_view, listView, false);
        ButterKnife.bind(this, view);
        listView.addHeaderView(view);
        initRadioEvent();
        rbToday.setChecked(true);
    }

    private void initRadioEvent() {
        rgDay.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_yesterday:
                        type = "0";
                        initHttp();
                        break;
                    case R.id.rb_today:
                        type = "1";
                        initHttp();
                        break;
                    case R.id.rb_7day:
                        type = "7";
                        initHttp();
                        break;
                    case R.id.rb_30day:
                        type = "30";
                        initHttp();
                        break;
                }
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.COUNT_INCOME)
                .addParams("type", type)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        IncomeBean = gson.fromJson(json, CountIncomeBean.class);
                        initView(IncomeBean.getData());
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


    public void refresh() {
        rbToday.setChecked(true);
        initHttp();
    }

    private void initView(CountIncomeBean.DataBean dataBean) {
        if (appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("3")) {
            tvChartCount.setText("总计" + dataBean.getInfo().getZ_s_payment());
            tvChart1Top.setText(dataBean.getInfo().getZ_cash_s_payment());
            tvChart1Bottom.setText(dataBean.getInfo().getZ_cash_s_payment_per() + "%");
            tvChart2Top.setText(dataBean.getInfo().getZ_bank_s_payment());
            tvChart2Bottom.setText(dataBean.getInfo().getZ_bank_s_payment_per() + "%");
            tvChart3Top.setText(dataBean.getInfo().getZ_alipay_s_payment());
            tvChart3Bottom.setText(dataBean.getInfo().getZ_alipay_s_payment_per() + "%");
            tvChart4Top.setText(dataBean.getInfo().getZ_wechat_s_payment());
            tvChart4Bottom.setText(dataBean.getInfo().getZ_wechat_s_payment_per() + "%");
            int length = getViewLength();//获取长条总共长度
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) (length * dataBean.getInfo().getZ_cash_s_payment_per()) / 100, ViewGroup.LayoutParams.MATCH_PARENT);
            viewChart1.setLayoutParams(layoutParams);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams((int) (length * dataBean.getInfo().getZ_bank_s_payment_per()) / 100, ViewGroup.LayoutParams.MATCH_PARENT);
            viewChart2.setLayoutParams(layoutParams1);
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams((int) (length * dataBean.getInfo().getZ_alipay_s_payment_per()) / 100, ViewGroup.LayoutParams.MATCH_PARENT);
            viewChart3.setLayoutParams(layoutParams2);
            LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams((int) (length * dataBean.getInfo().getZ_wechat_s_payment_per()) / 100, ViewGroup.LayoutParams.MATCH_PARENT);
            viewChart4.setLayoutParams(layoutParams3);
        } else {
            tvChartCount.setText("总计:--");
            tvChart1Top.setText("--");
            tvChart1Bottom.setText("--" + "%");
            tvChart2Top.setText("--");
            tvChart2Bottom.setText("--" + "%");
            tvChart3Top.setText("--");
            tvChart3Bottom.setText("--" + "%");
            tvChart4Top.setText("--");
            tvChart4Bottom.setText("--" + "%");
        }
    }

    /**
     * 获取长条宽度  屏幕宽度减去左右间距
     *
     * @return
     */
    private int getViewLength() {
        DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
        return dm.widthPixels - DensityUtil.dip2px(mActivity, 30);//左右间距为15dp
    }
}
